package com.moziware.demowebapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import java.util.Locale;

public class MainActivity extends AppCompatActivity {

    DemoWebView demoWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
    }

    private void init() {
        demoWebView = findViewById(R.id.demowebview);
        demoWebView.getSettings().setDomStorageEnabled(true);
        demoWebView.getSettings().setAllowFileAccess(true);
        demoWebView.getSettings().setAllowFileAccessFromFileURLs(true);
        demoWebView.getSettings().setAllowUniversalAccessFromFileURLs(true);
        demoWebView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
        demoWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                String url = request.getUrl().toString();
                if (url.startsWith("http://") || url.startsWith("https://") || url.startsWith("file:///")) {
                    view.loadUrl(url);
                }
                return true;
            }
        });
        String language = getLanguage();
        String myWebUrl;
        if ("zh".equals(language)){
            myWebUrl = "file:///android_asset/example/index_zh.html";
        }else {
            myWebUrl = "file:///android_asset/example/index_en.html";
        }
        demoWebView.loadUrl(myWebUrl);
    }

    private String getLanguage(){
        Locale locale = Locale.getDefault();
        return locale.getLanguage();
    }
}